﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;
using Telerik.Web.UI;

namespace EdgleyDesign10.Web
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (AcmsPage != null && AcmsPage.AcmsContext != null && AcmsPage.AcmsContext.CurrentPost != null)
                {
                    var pathParts = AcmsPage.AcmsContext.CurrentPost.Path.Split('/');
                    if (pathParts.Length > 1)
                        form1.Attributes["class"] = "page-" + pathParts[pathParts.Length - 1];
                    else
                        form1.Attributes["class"] = "home";
                }
            }
        }
        protected Acms.AcmsControls.TemplatePage AcmsPage
        {
            get
            {
                var tmp = this.Page as Acms.AcmsControls.TemplatePage;
                return tmp;
            }
        }

        protected AcmsContext AcmsContext
        {
            get
            {
                if (Page is TemplatePage)
                {
                    return ((TemplatePage)Page).AcmsContext;
                }
                return null;
            }
        }


        protected void subMenu_DataBinding(object sender, EventArgs e)
        {
            //find the parent path for the menu
            var menu = (Telerik.Web.UI.RadMenu)sender;
            var rptItem = (RepeaterItem)menu.NamingContainer;
            var post = (Post)rptItem.DataItem;
            var dataSource = (Acms.DataControls.AcmsSiteMapDataSource)rptItem.FindControl("dsSubMenu");
            dataSource.ParentPath = post.Path;
        }

        protected void menu_ItemDataBound(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {
            Post post = e.Item.DataItem as Post;
            if (post != null)
            {
                if (AcmsContext != null && AcmsContext.CurrentPost != null && AcmsContext.CurrentPost.Path.StartsWith(post.Path))
                {
                    e.Item.HighlightPath();
                }
            }
        }

        protected void subMenu_PreRender(object sender, EventArgs e)
        {
            var subMenu = (RadMenu)sender;
            foreach (RadMenuItem item in subMenu.Items)
            {
                if (item.Items.Count != 0)
                {
                    item.GroupSettings.OffsetX = 0;
                }
            }
        }

        private Acms.Bll.Menu _MainMenu;
        private Acms.Bll.Menu MainMenu
        {
            get
            {
                if (_MainMenu == null)
                {
                    _MainMenu = Acms.Bll.Menu.GetByNameAndSiteId("Main", AcmsContext.SiteId);
                    if (_MainMenu == null)
                    {
                        throw new Exception("Cannot load menu 'Main'");
                    }
                }
                return _MainMenu;
            }
        }

        protected void dsSubMenus_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
        {
            e.SetData(Post.GetAllBySiteIdMenuIdAndParentPath(AcmsContext.SiteId, MainMenu.Id, "~/", AcmsContext.SiteVersion));   
        }
    }
}
